package com.soft.springhrms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.soft.springhrms.mapper.ApprovesalaryMapper;
import com.soft.springhrms.mapper.EmployeesMapper;
import com.soft.springhrms.pojo.Approvesalary;
import com.soft.springhrms.pojo.Employees;
import com.soft.springhrms.pojo.Requests;
import com.soft.springhrms.service.RequestsService;
import com.soft.springhrms.mapper.RequestsMapper;
import com.soft.springhrms.vo.ApprovesalaryVO;
import com.soft.springhrms.vo.RequestVO;
import com.soft.springhrms.vo.ResultVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author DELL
 * @description 针对表【requests】的数据库操作Service实现
 * @createDate 2024-11-16 18:48:18
 */
@Service
public class RequestsServiceImpl extends ServiceImpl<RequestsMapper, Requests> implements RequestsService{
    @Autowired
    private ApprovesalaryMapper approvesalaryMapper;
    @Autowired
    private RequestsMapper requestsMapper;
    @Autowired
    private EmployeesMapper employeesMapper;

    //传入的Requests需要获取当前用户的员工id
    //请求类型，开始时间，结束时间，申请原因为用户表单必填项。
    @Override
    public ResultVO addRequests(Requests requests) {
        // 参数验证
        if (requests.getEmployeeId() == null || requests.getEmployeeId() <= 0) {
            return ResultVO.error(404, "无效的员工 ID");
        }
        requests.setCreatedAt(new Date());
        System.out.println(requests);
        save(requests);
        return ResultVO.success("添加成功");
    }

    //需要获取当前人事的员工id
    @Override
    public ResultVO querySalaryRequests(Integer hrId, int pageNum, int pageSize) {
        // 参数验证
        if (hrId == null || hrId <= 0) {
            return ResultVO.error(404, "无效的 HR ID");
        }
        try {
            // 创建分页对象
            Page<Approvesalary> page = new Page<>(pageNum, pageSize);

            // 设置查询条件
            QueryWrapper<Approvesalary> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("hr_id", hrId);

            // 执行分页查询
            IPage<Approvesalary> resultPage = approvesalaryMapper.selectPage(page, queryWrapper);

            // 判断结果是否为空
            if (resultPage.getRecords().isEmpty()) {
                return ResultVO.success(resultPage, "暂无数据");
            }


            List<ApprovesalaryVO> approvesalaryVOList = new ArrayList<>();

            // 遍历查询结果并补充 employeeName
            for (Approvesalary approvesalary : resultPage.getRecords()) {
                ApprovesalaryVO approvesalaryVO = new ApprovesalaryVO();
                System.out.println(approvesalary);
                BeanUtils.copyProperties(approvesalary, approvesalaryVO); // 将 Approvesalary 属性复制到 RequestVO
                System.out.println(approvesalaryVO);
                Integer employeeId = approvesalary.getEmployeeId();
                Employees employee = employeesMapper.selectById(employeeId); // 根据 employeeId 查询 Employee

                if (employee != null) {
                    approvesalaryVO.setEmployeeName(employee.getFullName()); // 设置 employeeName
                }

                approvesalaryVOList.add(approvesalaryVO);
            }

            // 包装数据并返回
            IPage<ApprovesalaryVO> approvesalaryVOPage = new Page<>();
            approvesalaryVOPage.setRecords(approvesalaryVOList);
            approvesalaryVOPage.setTotal(resultPage.getTotal());

            return ResultVO.success(approvesalaryVOPage, "查询成功");

        } catch (Exception e) {
            return ResultVO.error(404, e.getMessage());
        }
    }

    @Override
    public ResultVO queryRequestsHR(int pageNum, int pageSize) {
        try {
            // 创建分页对象
            Page<Requests> page = new Page<>(pageNum, pageSize);

            // 设置查询条件
            QueryWrapper<Requests> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("type", "请假", "加班"); // HR 处理的类型

            // 执行分页查询
            IPage<Requests> resultPage = requestsMapper.selectPage(page, queryWrapper);

            // 判断结果是否为空
            if (resultPage.getRecords().isEmpty()) {
                return ResultVO.success(resultPage, "暂无数据");
            }

            // 创建返回的 RequestVO 列表
            List<RequestVO> requestVOList = new ArrayList<>();

            // 通过 employeeId 查询对应的 fullname
            for (Requests request : resultPage.getRecords()) {
                RequestVO requestVO = new RequestVO();
                BeanUtils.copyProperties(request, requestVO); // 将 Requests 属性复制到 RequestVO

                Integer employeeId = request.getEmployeeId();
                Employees employee = employeesMapper.selectById(employeeId); // 根据 employeeId 查询 Employee

                if (employee != null) {
                    requestVO.setEmployeeName(employee.getFullName()); // 设置 fullname
                }

                requestVOList.add(requestVO);
            }

            // 包装数据并返回
            IPage<RequestVO> requestVOPage = new Page<>();
            requestVOPage.setRecords(requestVOList);
            requestVOPage.setTotal(resultPage.getTotal());

            return ResultVO.success(requestVOPage, "查询成功");

        } catch (Exception e) {
            return ResultVO.error(500, "查询失败: " + e.getMessage());
        }
    }

    @Override
    public ResultVO queryRequestsAdmin(int pageNum, int pageSize) {
        try {
            // 创建分页对象
            Page<Requests> page = new Page<>(pageNum, pageSize);

            // 设置查询条件
            QueryWrapper<Requests> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("type", "离职"); // 管理员处理的类型

            // 执行分页查询
            IPage<Requests> resultPage = requestsMapper.selectPage(page, queryWrapper);

            // 判断结果
            if (resultPage.getRecords().isEmpty()) {
                return ResultVO.success(resultPage, "暂无数据");
            }

            // 创建返回的 RequestVO 列表
            List<RequestVO> requestVOList = new ArrayList<>();

            // 通过 employeeId 查询对应的 fullname
            for (Requests request : resultPage.getRecords()) {
                RequestVO requestVO = new RequestVO();
                BeanUtils.copyProperties(request, requestVO); // 将 Requests 属性复制到 RequestVO

                Integer employeeId = request.getEmployeeId();
                Employees employee = employeesMapper.selectById(employeeId); // 根据 employeeId 查询 Employee

                if (employee != null) {
                    requestVO.setEmployeeName(employee.getFullName()); // 设置 fullname
                }

                requestVOList.add(requestVO);
            }
            // 包装数据并返回
            IPage<RequestVO> requestVOPage = new Page<>();
            requestVOPage.setRecords(requestVOList);
            requestVOPage.setTotal(resultPage.getTotal());

            return ResultVO.success(requestVOPage, "查询成功");
        } catch (Exception e) {
            return ResultVO.error(500, "查询失败: " + e.getMessage());
        }
    }

    @Override
    public ResultVO approveRequestsHR(Requests requests) {
        // 更新状态
        int rows = requestsMapper.updateById(requests);
        if (rows > 0) {
            return ResultVO.success("审批成功");
        } else {
            return ResultVO.error(404, "未找到对应请求记录");
        }
    }

    @Override
    public ResultVO approveRequestsAdmin(Requests requests) {
        if (!"离职".equals(requests.getType())) {
            return ResultVO.error(403, "管理员只能审批离职请求");
        }

        // 更新状态
        int rows = requestsMapper.updateById(requests);
        if (rows > 0) {
            return ResultVO.success("审批成功");
        } else {
            return ResultVO.error(404, "未找到对应请求记录");
        }
    }

    @Override
    public ResultVO queryRequestsHistory(Integer employeeId, int pageNum, int pageSize) {
        try {
            // 创建分页对象，pageNum 从1开始
            Page<Requests> page = new Page<>(pageNum, pageSize);

            // 设置查询条件
            QueryWrapper<Requests> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("employee_id", employeeId);

            // 执行分页查询
            IPage<Requests> resultPage = requestsMapper.selectPage(page, queryWrapper);

            // 判断结果是否为空
            if (resultPage.getRecords().isEmpty()) {
                return ResultVO.success(resultPage, "暂无数据");
            }

            // 直接使用查询结果，不需要手动复制
            return ResultVO.success(resultPage, "查询成功");

        } catch (Exception e) {
            return ResultVO.error(500, "查询失败: " + e.getMessage());
        }
    }


}




